package com.kdzy.lshy.socity_server.service;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kdzy.lshy.socity_server.common.Constants;
import com.kdzy.lshy.socity_server.common.ResultMap;
import com.kdzy.lshy.socity_server.repository.BaseDao;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 我的小组查询
 *  1.我的小组查询（工作量统计）
 *  2.我的小组查询（具体工作内容）
 *  3.我的小组详情（具体工作内容）
 *  4.办事结果更新
 *  5.工作端办事结果查询
 *  6.居民查询办事结果
 *
 * @author sjj
 */

@Service
public class MyTeamService extends BaseService{
    @Autowired
    private BaseDao baseDao;

    private static final Logger logger = LoggerFactory.getLogger(MyTeamService.class);

    //我的小组查询（工作量统计）
    public Object demandMyTeamWorkload(String param, HttpServletRequest request){
        String status = null;
        String info = null;
        Object content = null;
        List list = new ArrayList();

        //获取解析JSON字符串
        JSONObject jsonObject = JSON.parseObject(param);

        //**********************************************************
        if (null == jsonObject) {
            status = Constants.http_status_fail;
            info = Constants.http_info_fali;
            content = "参数为空";
            return ResultMap.creatReault(status, info, content);
        }

        LinkedHashMap<Object, Object> map = null;

        String systoken = null;
        if (!(jsonObject.getInteger("visit_plat") != 0)) { // 网页端
            systoken = jsonObject.getString("systoken"); // 全局token
            if (StringUtils.isBlank(systoken)) {
                logger.info("systoken为空");
                map = ResultMap.creatReault("33", "失败", "token为空");
                logger.info("出去《我的小组查询（工作量统计）》demandMyTeamWorkload()方法；出参 = " + JSON.toJSONString(map));
                return map;
            }
        }

        String officerId = null;
        if (jsonObject.getInteger("visit_plat") == 0) { // 网页端

            // 根据身份证从redis中获取token
            Jedis jedis = new Jedis("140.143.210.101", 6379);
            jedis.auth("q1w2e3r4t5");

            List<String> infoList = jedis.hmget(systoken, "systoken", "officerId");
            String systokenRedis = null;
            if (infoList != null && infoList.size() > 0) {
                systokenRedis = infoList.get(0); // token
                officerId = infoList.get(1); // 用户id
            }

            // 查看token是否过期
            if (StringUtils.isBlank(systokenRedis)) { // 重新登录，弹出登录框
                logger.info("全局systoken过期，需要重新获取systoken");
                map = ResultMap.creatReault("33", "失败", "systoken过期");
                logger.info("出去《我的小组查询（工作量统计）》demandMyTeamWorkload()方法；出参 = " + JSON.toJSONString(map));
                return map;
            }

            // token不一致
            if (!StringUtils.equals(systokenRedis, systoken)) {
                logger.info("用户访问的token不一致");
                map = ResultMap.creatReault("331", "失败", "systoken不一致");
                logger.info("出去《我的小组查询（工作量统计）》demandMyTeamWorkload()方法；出参 = " + JSON.toJSONString(map));
                return map;
            }

        }
        //**********************************************************

        //StringBuffer动态写sql语句
        StringBuffer sql = new StringBuffer(
                "SELECT \n" +
                "\tb.bid,\n" +
                "\tb.officer_id,\n" +
                "\tb.officer_number,\n" +
                "\tb.officer_name,\n" +
                "\tb.`name`,\n" +
                "\tb.dep_name,\n" +
                "\tcount(b.officer_id) AS sum,\n" +
                "\tSUM(b.bizz_type = 1) AS sum1,\n" +
                "\tSUM(b.bizz_type = 2) AS sum2,\n" +
                "\tSUM(b.bizz_type = 3) AS sum3,\n" +
                "\tSUM(b.bizz_type = 4) AS sum4,\n" +
                "\tSUM(b.bizz_type = 5) AS sum5,\n" +
                "\tSUM(b.bizz_type = 6) AS sum6,\n" +
                "\tSUM(b.bizz_type = 7) AS sum7,\n" +
                "\tSUM(b.bizz_type = 8) AS sum8,\n" +
                "\tSUM(b.bizz_type = 9) AS sum9\n" +
                "FROM\n" +
                "\t(\n" +
                "\t\tSELECT DISTINCT\n" +
                "\t\t\to.officer_number,\n" +
                "\t\t\to.officer_name,\n" +
                "\t\t\tdt.dep_name,\n" +
                "\t\t\tb.id bid,\n" +
                "\t\t\tb.bizz_type,\n" +
                "\t\t\tod.officer_id,\n" +
                "\t\t\tr.`name`\n" +
                "\t\tFROM\n" +
                "\t\t\tdepartments dt,\n" +
                "\t\t\tbespeak b,\n" +
                "\t\t\tofficer o,\n" +
                "\t\t\tofficer_department od,\n" +
                "\t\t\tofficer_role off_rol,\n" +
                "\t\t\trole r\n" +
                "\t\tWHERE\n" +
                "\t\tdt.ID = b.dep_id\n" +
                "\t\tAND od.officer_id = o.ID\n" +
                "\t\tAND b.officer_id = o.ID\n" +
                "\t\tAND o.ID = off_rol.officer_id\n" +
                "\t\tAND off_rol.role_id = r.ID\n" +
                "\t\tAND b.version = ?\n" +
                "\t\tAND b.visit_type = ?\n" +
                " \t\tAND b.is_used = ?\n" +
                "\t\tAND\n" +
                "\t\tIF (\n" +
                "\t\t\t? NOT IN (\n" +
                "\t\t\t\tSELECT\n" +
                "\t\t\t\t\toffr.officer_id\n" +
                "\t\t\t\tFROM\n" +
                "\t\t\t\t\tofficer_role offr\n" +
                "\t\t\t\tWHERE\n" +
                "\t\t\t\t\toffr.role_id IN ('leader18f705d08db6479d8e14b8d11b')\n" +
                "\t\t\t),\n" +
                "\t\t\t(\n" +
                "\n" +
                "\t\t\t\tIF (\n" +
                "\t\t\t\t\t(\n" +
                "\t\t\t\t\t\t? IN (\n" +
                "\t\t\t\t\t\t\tSELECT\n" +
                "\t\t\t\t\t\t\t\tod.officer_id\n" +
                "\t\t\t\t\t\t\tFROM\n" +
                "\t\t\t\t\t\t\t\tofficer_department od\n" +
                "\t\t\t\t\t\t\tWHERE\n" +
                "\t\t\t\t\t\t\t\tod.officer_id NOT IN (\n" +
                "\t\t\t\t\t\t\t\t\tSELECT DISTINCT\n" +
                "\t\t\t\t\t\t\t\t\t\todd.parent_id\n" +
                "\t\t\t\t\t\t\t\t\tFROM\n" +
                "\t\t\t\t\t\t\t\t\t\tofficer_department odd\n" +
                "\t\t\t\t\t\t\t\t)\n" +
                "\t\t\t\t\t\t)\n" +
                "\t\t\t\t\t),\n" +
                "\t\t\t\t\t(od.officer_id = ?),\n" +
                "\t\t\t\t\t(od.parent_id = ?)\n" +
                "\t\t\t\t)\n" +
                "\t\t\t),\n" +
                "\t\t\t(1 = 1)\n" +
                "\t\t)\n" +
                "\t) b");

        List<Object> tags = new ArrayList<Object>();
        if (StringUtils.isNoneBlank(jsonObject.getString("version"))){
            tags.add(Integer.parseInt(jsonObject.getString("version")));
        }
        if (StringUtils.isNoneBlank(jsonObject.getString("visit_type"))){
            tags.add(Integer.parseInt(jsonObject.getString("visit_type")));
        }
        if (StringUtils.isNoneBlank(jsonObject.getString("is_used"))){
            tags.add(Integer.parseInt(jsonObject.getString("is_used")));
        }
        if (StringUtils.isNoneBlank(jsonObject.getString("officer_id"))) {
            Object officer_id = jsonObject.getString("officer_id");
            for (int i = 0; i < 4; i++){
                tags.add(officer_id);
            }
        } else {
            status = Constants.http_status_fail;
            info = Constants.http_info_fali;
            content = "参数为空";
            return ResultMap.creatReault(status,info,content);
        }
        //条件搜索，办事员姓名
        if (StringUtils.isNoneBlank(jsonObject.getString("officer_name"))) {
            sql.append(" WHERE b.officer_name LIKE ?");
            tags.add("%" + jsonObject.getString("officer_name") + "%");
        }
        sql.append(" GROUP BY\n" +
                "\tb.officer_id\n" +
                "ORDER BY\n" +
                "\tb.dep_name");
        //分页
        Integer page = Integer.valueOf(request.getParameter("offset"));
        Integer rows = Integer.valueOf(request.getParameter("limit"));

        long total = 0;
        String countsql = "select count(1) as count from (" + sql.toString() + ") as t";
        try {
            total = baseDao.getCount(countsql,tags);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }

        int start = page;

        String sql1 = sql.toString() + " limit " + start + "," + rows + "";

        try {
            list = baseDao.queryForList(sql1,tags.toArray());
            status = Constants.http_status_success;
            info = Constants.http_info_success;
            content = list;

        } catch (Exception e) {
            logger.error(e.getMessage());
            status = Constants.http_status_fail;
            info = Constants.http_info_fali;
            content = "后台处理异常";
        }

        map = ResultMap.creatReault(status,info,content);
        map.put("total",total);

        return map;
    }


    //我的小组查询（具体工作内容）
    public Object demandMyTeam(String param, HttpServletRequest request) {

        String status = null;
        String info = null;
        Object content = null;
        List list = new ArrayList();

        //获取解析JSON字符串
        JSONObject jsonObject = JSON.parseObject(param);

        //**********************************************************
        if (null == jsonObject) {
            status = Constants.http_status_fail;
            info = Constants.http_info_fali;
            content = "参数为空";
            return ResultMap.creatReault(status, info, content);
        }

        LinkedHashMap<Object, Object> map = null;

        String systoken = null;
        if (!(jsonObject.getInteger("visit_plat") != 0)) { // 网页端
            systoken = jsonObject.getString("systoken"); // 全局token
            if (StringUtils.isBlank(systoken)) {
                logger.info("systoken为空");
                map = ResultMap.creatReault("33", "失败", "token为空");
                logger.info("出去《我的小组查询（具体工作内容）》demandMyTeam()方法；出参 = " + JSON.toJSONString(map));
                return map;
            }
        }

        String officerId = null;
        if (jsonObject.getInteger("visit_plat") == 0) { // 网页端

            // 根据身份证从redis中获取token
            Jedis jedis = new Jedis("140.143.210.101", 6379);
            jedis.auth("q1w2e3r4t5");

            List<String> infoList = jedis.hmget(systoken, "systoken", "officerId");
            String systokenRedis = null;
            if (infoList != null && infoList.size() > 0) {
                systokenRedis = infoList.get(0); // token
                officerId = infoList.get(1); // 用户id
            }

            // 查看token是否过期
            if (StringUtils.isBlank(systokenRedis)) { // 重新登录，弹出登录框
                logger.info("全局systoken过期，需要重新获取systoken");
                map = ResultMap.creatReault("33", "失败", "systoken过期");
                logger.info("出去《我的小组查询（具体工作内容）》demandMyTeam()方法；出参 = " + JSON.toJSONString(map));
                return map;
            }

            // token不一致
            if (!StringUtils.equals(systokenRedis, systoken)) {
                logger.info("用户访问的token不一致");
                map = ResultMap.creatReault("331", "失败", "systoken不一致");
                logger.info("出去《我的小组查询（具体工作内容）》demandMyTeam()方法；出参 = " + JSON.toJSONString(map));
                return map;
            }

        }
        //**********************************************************

        //StringBuffer动态写sql语句
        StringBuffer sql = new StringBuffer("SELECT b.ID,o.officer_number,o.officer_name,b.time,bz.bizz_name,b.bizz_type" +
                                            " FROM bespeak b,dweller d,bizz bz,departments dt,officer_department od,officer o" +
                                            " WHERE d.ID = b.dweller_id AND bz.ID = b.bizz_id AND dt.ID = od.dep_id AND dt.ID = b.dep_id AND o.ID = b.officer_id");
        List<Object> tags = new ArrayList<Object>();

        if (StringUtils.isNoneBlank(jsonObject.getString("officer_id"))) {
            sql.append(" AND od.officer_id = ?");
            tags.add(jsonObject.getString("officer_id"));
        } else {
            status = Constants.http_status_fail;
            info = Constants.http_info_fali;
            content = "参数为空";
            return ResultMap.creatReault(status,info,content);
        }
        if (StringUtils.isNoneBlank(jsonObject.getString("officer_name"))) {
            sql.append(" AND d.officer_name LIKE ?");
            tags.add("%"+jsonObject.getString("officer_name")+"%");
        }
        if (StringUtils.isNoneBlank(jsonObject.getString("time"))) {
            sql.append(" AND b.time = ?");
            tags.add(jsonObject.getString("time"));
        }
        if (StringUtils.isNoneBlank(jsonObject.getString("bizz_name"))) {
            sql.append(" AND bz.bizz_name LIKE ?");
            tags.add("%" + jsonObject.getString("bizz_name") + "%");
        }
        if (StringUtils.isNoneBlank(jsonObject.getString("bizz_type"))) {
            sql.append(" AND b.bizz_type = ?");
            tags.add(Integer.parseInt(jsonObject.getString("bizz_type")));
        }
        sql.append(" order by b.create_time desc");

        //分页
        Integer page = Integer.valueOf(request.getParameter("offset"));
        Integer rows = Integer.valueOf(request.getParameter("limit"));

        long total = 0;
        String countsql = "select count(1) as count from (" + sql.toString() + ") as t";
        try {
            total = baseDao.getCount(countsql,tags);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }

        int start = page;

        String sql1 = sql.toString() + " limit " + start + "," + rows + "";

        try {
            content = baseDao.queryForList(sql1,tags.toArray());
            status = Constants.http_status_success;
            info = Constants.http_info_success;

        } catch (Exception e) {
            logger.error(e.getMessage());
            status = Constants.http_status_fail;
            info = Constants.http_info_fali;
            content = "后台处理异常";
        }

        map = ResultMap.creatReault(status,info,content);
        map.put("total",total);

        return map;
    }


    //我的小组详情
    public Object myTeamDetails(String param,HttpServletRequest request) {

        List list = new ArrayList();
        String status = null;
        String info = null;
        Object content = null;

        //获取解析JSON字符串
        JSONObject jsonObject = JSON.parseObject(param);

        //**********************************************************
        if (null == jsonObject) {
            status = Constants.http_status_fail;
            info = Constants.http_info_fali;
            content = "参数为空";
            return ResultMap.creatReault(status, info, content);
        }

        LinkedHashMap<Object, Object> map = null;

        String systoken = null;
        if (!(jsonObject.getInteger("visit_plat") != 0)) { // 网页端
            systoken = jsonObject.getString("systoken"); // 全局token
            if (StringUtils.isBlank(systoken)) {
                logger.info("systoken为空");
                map = ResultMap.creatReault("33", "失败", "token为空");
                logger.info("出去《我的小组详情》myTeamDetails()方法；出参 = " + JSON.toJSONString(map));
                return map;
            }
        }

        String officerId = null;
        if (jsonObject.getInteger("visit_plat") == 0) { // 网页端

            // 根据身份证从redis中获取token
            Jedis jedis = new Jedis("140.143.210.101", 6379);
            jedis.auth("q1w2e3r4t5");

            List<String> infoList = jedis.hmget(systoken, "systoken", "officerId");
            String systokenRedis = null;
            if (infoList != null && infoList.size() > 0) {
                systokenRedis = infoList.get(0); // token
                officerId = infoList.get(1); // 用户id
            }

            // 查看token是否过期
            if (StringUtils.isBlank(systokenRedis)) { // 重新登录，弹出登录框
                logger.info("全局systoken过期，需要重新获取systoken");
                map = ResultMap.creatReault("33", "失败", "systoken过期");
                logger.info("出去《我的小组详情》myTeamDetails()方法；出参 = " + JSON.toJSONString(map));
                return map;
            }

            // token不一致
            if (!StringUtils.equals(systokenRedis, systoken)) {
                logger.info("用户访问的token不一致");
                map = ResultMap.creatReault("331", "失败", "systoken不一致");
                logger.info("出去《我的小组详情》myTeamDetails()方法；出参 = " + JSON.toJSONString(map));
                return map;
            }

        }
        //**********************************************************

        //StringBuffer动态写sql语句
        StringBuffer sql = new StringBuffer("SELECT DISTINCT b.ID,b.time,o.officer_number,bz.bizz_name,o.officer_name,b.is_socity_bizz,d.dweller_name,b.bizz_type,d.dweller_idcard,b.update_time " +
                                            "FROM bespeak b,dweller d,bizz bz,departments dt,officer_department od,officer o " +
                                            "WHERE d.ID = b.dweller_id AND bz.ID = b.bizz_id AND dt.ID = od.dep_id AND dt.ID = b.dep_id AND o.ID = b.officer_id");
        List<Object> tags = new ArrayList<Object>();

        if (StringUtils.isNoneBlank(jsonObject.getString("id"))) {
            sql.append(" AND b.ID = ?");
            tags.add(jsonObject.getString("id"));
        } else {
            status = Constants.http_status_fail;
            info = Constants.http_info_fali;
            content = "参数为空";
            return ResultMap.creatReault(status,info,content);
        }

        try {
            list = baseDao.queryForList(sql.toString(), tags.toArray());
            status = Constants.http_status_success;
            info = Constants.http_info_success;
            content = list;

        } catch (Exception e) {
            logger.error(e.getMessage());
            status = Constants.http_status_fail;
            info = Constants.http_info_fali;
            content = "后台处理异常";
        }
        return ResultMap.creatReault(status, info, content);
    }


    //办事结果更新
    public Object update(String param, HttpServletRequest request) {

        List list = new ArrayList();
        String status = null;
        String info = null;
        Object content = null;


        //获取解析JSON字符串
        JSONObject jsonObject = JSON.parseObject(param);

        //**********************************************************
        if (null == jsonObject) {
            status = Constants.http_status_fail;
            info = Constants.http_info_fali;
            content = "参数为空";
            return ResultMap.creatReault(status, info, content);
        }

        LinkedHashMap<Object, Object> map = null;

        String systoken = null;
        if (!(jsonObject.getInteger("visit_plat") != 0)) { // 网页端
            systoken = jsonObject.getString("systoken"); // 全局token
            if (StringUtils.isBlank(systoken)) {
                logger.info("systoken为空");
                map = ResultMap.creatReault("33", "失败", "token为空");
                logger.info("出去《办事结果更新》workResultUpdate()方法；出参 = " + JSON.toJSONString(map));
                return map;
            }
        }

        String officerId = null;
        if (jsonObject.getInteger("visit_plat") == 0) { // 网页端

            // 根据身份证从redis中获取token
            Jedis jedis = new Jedis("140.143.210.101", 6379);
            jedis.auth("q1w2e3r4t5");

            List<String> infoList = jedis.hmget(systoken, "systoken", "officerId");
            String systokenRedis = null;
            if (infoList != null && infoList.size() > 0) {
                systokenRedis = infoList.get(0); // token
                officerId = infoList.get(1); // 用户id
            }

            // 查看token是否过期
            if (StringUtils.isBlank(systokenRedis)) { // 重新登录，弹出登录框
                logger.info("全局systoken过期，需要重新获取systoken");
                map = ResultMap.creatReault("33", "失败", "systoken过期");
                logger.info("出去《办事结果更新》workResultUpdate()方法；出参 = " + JSON.toJSONString(map));
                return map;
            }

            // token不一致
            if (!StringUtils.equals(systokenRedis, systoken)) {
                logger.info("用户访问的token不一致");
                map = ResultMap.creatReault("331", "失败", "systoken不一致");
                logger.info("出去《办事结果更新》workResultUpdate()方法；出参 = " + JSON.toJSONString(map));
                return map;
            }

        }
        //**********************************************************

        //StringBuffer动态写sql语句
        StringBuffer sql = new StringBuffer("UPDATE bespeak b SET");
        List<Object> tags = new ArrayList<Object>();
        //解析修改业务状态
        if (StringUtils.isNoneBlank(jsonObject.getString("bizz_type"))) {
            sql.append(" b.bizz_type = ?");
            tags.add(Integer.parseInt(jsonObject.getString("bizz_type")));
        }
        //解析修改失败原因
        if (StringUtils.isNoneBlank(jsonObject.getString("bizz_fall_res"))){
            sql.append(" ,b.bizz_fall_res = ?");
            tags.add(jsonObject.getString("bizz_fall_res"));
        }
        //解析修改成功补充说明
        if (StringUtils.isNoneBlank(jsonObject.getString("bizz_succ_note"))){
            sql.append(" ,b.bizz_succ_note = ?");
            tags.add(jsonObject.getString("bizz_succ_note"));
        }
        //解析修改时间戳及时间戳转换的更新时间
        if (StringUtils.isNoneBlank(jsonObject.getLong("stamp") + "")) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String sd = sdf.format(new Date(Long.parseLong(jsonObject.getLong("stamp") + "")));
            sql.append(" ,b.update_time = ?");
            tags.add(sd);
            sql.append(" ,b.stamp = ?");
            tags.add(jsonObject.getLong("stamp") + "");
        }
        if (StringUtils.isNoneBlank(jsonObject.getString("id"))) {
            sql.append(" WHERE b.ID = ?");
            tags.add(jsonObject.getString("id"));
        } else {
            status = Constants.http_status_fail;
            info = Constants.http_info_fali;
            content = "参数为空";
            return ResultMap.creatReault(status,info,content);
        }

        try {
            if (baseDao.update(sql.toString(), tags.toArray()) !=0){
                status = Constants.http_status_success;
                info = Constants.http_info_success;
                content ="success";
            }else {
                status = Constants.http_status_fail;
                info = Constants.http_info_fali;
                content = "fali";
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            status = Constants.http_status_fail;
            info = Constants.http_info_fali;
            content = "后台处理异常";
        }
        return ResultMap.creatReault(status,info,content);
    }


    //工作端办事结果查询
    public Object officerWorkResult(String param, HttpServletRequest request) {


        List list = new ArrayList();
        String status = null;
        String info = null;
        Object content = null;

        //获取解析JSON字符串
        JSONObject jsonObject = JSON.parseObject(param);

        //**********************************************************
        if (null == jsonObject) {
            status = Constants.http_status_fail;
            info = Constants.http_info_fali;
            content = "参数为空";
            return ResultMap.creatReault(status, info, content);
        }

        LinkedHashMap<Object, Object> map = null;

        String systoken = null;
        if (!(jsonObject.getInteger("visit_plat") != 0)) { // 网页端
            systoken = jsonObject.getString("systoken"); // 全局token
            if (StringUtils.isBlank(systoken)) {
                logger.info("systoken为空");
                map = ResultMap.creatReault("33", "失败", "token为空");
                logger.info("出去《工作端办事结果查询》officerWorkResult()方法；出参 = " + JSON.toJSONString(map));
                return map;
            }
        }

        String officerId = null;
        if (jsonObject.getInteger("visit_plat") == 0) { // 网页端

            // 根据身份证从redis中获取token
            Jedis jedis = new Jedis("140.143.210.101", 6379);
            jedis.auth("q1w2e3r4t5");

            List<String> infoList = jedis.hmget(systoken, "systoken", "officerId");
            String systokenRedis = null;
            if (infoList != null && infoList.size() > 0) {
                systokenRedis = infoList.get(0); // token
                officerId = infoList.get(1); // 用户id
            }

            // 查看token是否过期
            if (StringUtils.isBlank(systokenRedis)) { // 重新登录，弹出登录框
                logger.info("全局systoken过期，需要重新获取systoken");
                map = ResultMap.creatReault("33", "失败", "systoken过期");
                logger.info("出去《工作端办事结果查询》officerWorkResult()方法；出参 = " + JSON.toJSONString(map));
                return map;
            }

            // token不一致
            if (!StringUtils.equals(systokenRedis, systoken)) {
                logger.info("用户访问的token不一致");
                map = ResultMap.creatReault("331", "失败", "systoken不一致");
                logger.info("出去《工作端办事结果查询》officerWorkResult()方法；出参 = " + JSON.toJSONString(map));
                return map;
            }

        }
        //**********************************************************

        //动态增加SQL
        StringBuffer sql = new StringBuffer("SELECT b.ID,o.officer_name,b.time,d.dweller_name,dpt.dep_name,bz.bizz_name,b.is_socity_bizz,b.bizz_type " +
                                            "FROM bespeak b,dweller d,officer o,departments dpt,bizz bz " +
                                            "WHERE b.dweller_id = d.ID AND b.officer_id = o.ID AND b.dep_id = dpt.ID AND b.bizz_id = bz.ID");
        //创建动态参数集合
        List<Object> tags = new ArrayList<Object>();
        if (StringUtils.isNoneBlank(jsonObject.getString("id"))) {
            sql.append(" AND b.ID = ?");
            tags.add(jsonObject.getString("id"));
        } else {
            status = Constants.http_status_fail;
            info = Constants.http_info_fali;
            content = "参数为空";
            return ResultMap.creatReault(status,info,content);
        }

        try {
            list = baseDao.queryForList(sql.toString(),tags.toArray());
            status = Constants.http_status_success;
            info = Constants.http_info_success;
            content = list;
        } catch (Exception e) {
            logger.error(e.getMessage());
            status = Constants.http_status_fail;
            info = Constants.http_info_fali;
            content = "后台处理异常";
        }
        return ResultMap.creatReault(status, info, content);
    }


    //居民查询办事结果
    public Object dwellerWorkResult(String param, HttpServletRequest request) {


        List list = new ArrayList();
        String status = null;
        String info = null;
        Object content = null;

        //获取解析JSON字符串
        JSONObject jsonObject = JSON.parseObject(param);

        //**********************************************************
        if (null == jsonObject) {
            status = Constants.http_status_fail;
            info = Constants.http_info_fali;
            content = "参数为空";
            return ResultMap.creatReault(status, info, content);
        }

        LinkedHashMap<Object, Object> map = null;

        String systoken = null;
        if (!(jsonObject.getInteger("visit_plat") != 0)) { // 网页端
            systoken = jsonObject.getString("systoken"); // 全局token
            if (StringUtils.isBlank(systoken)) {
                logger.info("systoken为空");
                map = ResultMap.creatReault("33", "失败", "token为空");
                logger.info("出去《居民查询办事结果》dwellerWorkResult()方法；出参 = " + JSON.toJSONString(map));
                return map;
            }
        }

        String dwellerIdcard = null;
        String dwellerId = null;
        if (jsonObject.getInteger("visit_plat") == 0) { // 网页端

            // 根据身份证从redis中获取token
            Jedis jedis = new Jedis("140.143.210.101", 6379);
            jedis.auth("q1w2e3r4t5");

            List<String> infoList = jedis.hmget(systoken, "systoken",  "dwellerIdcard", "dwellerId");
            String systokenRedis = null;
            if (infoList != null && infoList.size() > 0) {
                systokenRedis = infoList.get(0); // token
                dwellerIdcard = infoList.get(1); // 居民身份证号
                dwellerId = infoList.get(2); // 居民唯一标识
            }

            // 查看token是否过期
            if (StringUtils.isBlank(systokenRedis)) { // 重新登录，弹出登录框
                logger.info("全局systoken过期，需要重新获取systoken");
                map = ResultMap.creatReault("33", "失败", "systoken过期");
                logger.info("出去《居民查询办事结果》dwellerWorkResult()方法；出参 = " + JSON.toJSONString(map));
                return map;
            }

            // token不一致
            if (!StringUtils.equals(systokenRedis, systoken)) {
                logger.info("用户访问的token不一致");
                map = ResultMap.creatReault("331", "失败", "systoken不一致");
                logger.info("出去《居民查询办事结果》dwellerWorkResult()方法；出参 = " + JSON.toJSONString(map));
                return map;
            }

        }
        //**********************************************************

        //动态增加SQL
        StringBuffer sql = new StringBuffer("SELECT b.ID,d.dweller_name,dpt.dep_name,bz.bizz_name,b.bizz_type,b.bizz_fall_res,b.bizz_succ_note " +
                                            "FROM bespeak b,dweller d,departments dpt,bizz bz " +
                                            "WHERE b.dweller_id = d.ID AND b.dep_id = dpt.ID AND b.bizz_id = bz.ID");
        //创建动态参数集合
        List<Object> tags = new ArrayList<Object>();
        if (StringUtils.isNoneBlank(jsonObject.getString("id"))) {
            sql.append(" AND b.ID = ?");
            tags.add(jsonObject.getString("id"));
        } else {
            status = Constants.http_status_fail;
            info = Constants.http_info_fali;
            content = "参数为空";
            return ResultMap.creatReault(status,info,content);
        }

        try {
            content = baseDao.queryForList(sql.toString(), tags.toArray());
            status = Constants.http_status_success;
            info = Constants.http_info_success;
        } catch (Exception e) {
            logger.error(e.getMessage());
            status = Constants.http_status_fail;
            info = Constants.http_info_fali;
            content = "后台处理异常";
        }

        return ResultMap.creatReault(status, info, content);
    }
}
